package com.jcf.springboot.common.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

/**
 * @author test
 * @since 2022/7/27
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

  @Bean
  public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
    RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(redisConnectionFactory);

    // 设置key和value的序列化规则
    redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
    redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    // 设置hashKey和hashValue的序列化规则
    redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
    redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());


  ;

    return redisTemplate;

  }
}
